package com.czq.zk3.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonUtils {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);

//    public Map<String , Object> bean2Map(){
//
//    }


    public static <T> T jsonNodeToPojo(String jsonData, String nodeName, Class<T> beanType) {
        try {
            JsonNode root = MAPPER.readTree(jsonData);
            JsonNode jsonNode = root.get(nodeName);
            System.out.println(jsonNode.asText());
            return MAPPER.readValue(jsonNode.traverse(), beanType);
        } catch (IOException e) {
            LOG.info("get node failed");
        }
        return null;
    }


    public static <T> List<T> jsonNodeToList(String jsonData, String nodeName, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            JsonNode root = MAPPER.readTree(jsonData);
            JsonNode jsonNode = root.get(nodeName);
            System.out.println(jsonNode.asText());
            return MAPPER.readValue(jsonNode.traverse(), javaType);
        } catch (IOException e) {
            LOG.info("get node failed");
        }
        return null;
    }


    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            LOG.error("objectToJson error", e);
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            LOG.error("jsonToPojo error", e);
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(jsonData, javaType);
            return list;
        } catch (Exception e) {
            LOG.error("jsonToList error", e);
        }
        return null;
    }


    //    public static void main(String[] args) {
//        String jsonArray = "[{\"id\":1,\"name\":\"产品\"},{\"id\":2,\"name\":\"测试\"},{\"id\":3,\"name\":\"开发\"},{\"id\":4,\"name\":\"UI\"},{\"id\":5,\"name\":\"运营\"}]";
//        // 将json数组转化成集合
//        List<Dept> depts = JsonUtils.jsonToList(jsonArray, Dept.class);
//
//        System.out.println(depts);
//
//        String jsonObj = "{\"id\":1,\"name\":\"产品\"}";
//        // 把json 对象转化成java对象
//        Dept dept = JsonUtils.jsonToPojo(jsonObj, Dept.class);
//        /**
//         * 什么是Json数组 [ ]
//         * 什么是Json对象 { }
//         */
//        System.out.println(dept);
//    }
//    public static void main(String[] args) {
//        String json = "{\n" +
//                "  \"code\": 200,\n" +
//                "  \"msg\": \"请求成功\",\n" +
//                "  \"result\": [\n" +
//                "    {\n" +
//                "      \"id\": 1,\n" +
//                "      \"name\": \"产品\"\n" +
//                "    },\n" +
//                "    {\n" +
//                "      \"id\": 2,\n" +
//                "      \"name\": \"测试\"\n" +
//                "    },\n" +
//                "    {\n" +
//                "      \"id\": 3,\n" +
//                "      \"name\": \"开发\"\n" +
//                "    },\n" +
//                "    {\n" +
//                "      \"id\": 4,\n" +
//                "      \"name\": \"UI\"\n" +
//                "    },\n" +
//                "    {\n" +
//                "      \"id\": 5,\n" +
//                "      \"name\": \"运营\"\n" +
//                "    }\n" +
//                "  ]\n" +
//                "}";
//
//        List<Dept> result = JsonUtils.jsonNodeToList(json, "result", Dept.class);
//        System.out.println("result = " + result);
//
//
//
//    }



}